<template>
  <div class="JADP-common-layout">
    <div class="JADP-common-layout-center JADP-flex-main">
      <el-row class="JADP-common-search-box" :gutter="16">
        <el-form @submit.native.prevent>
          <el-col :span="6">
            <el-form-item :label="$t('common.keyWord')">
              <el-input v-model="params.keyword" :placeholder="$t('common.enterKeyword')" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="search()">
                {{ $t("common.search") }}</el-button>
              <el-button icon="el-icon-refresh-right" @click="reset()">{{ $t("common.reset") }}
              </el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="JADP-common-layout-main JADP-flex-main">
        <div class="JADP-common-head">
          <div class="JADP-common-head-right">
            <el-tooltip effect="dark" :content="$t('common.refresh')" placement="top">
              <el-link icon="icon-ym icon-ym-Refresh JADP-common-head-icon" :underline="false" @click="reset()" />
            </el-tooltip>
            <screenfull />
          </div>
        </div>
        <JADP-table v-loading="listLoading" :data="tableData">
          <el-table-column prop="account" label="账号" width="100" />
          <el-table-column prop="realName" label="姓名" width="100" />
          <el-table-column label="性别" width="90" align="center">
            <template slot-scope="scope" sortable>
              <span>{{
        scope.row.gender == 1
          ? "男"
          : scope.row.gender == 2
            ? "女"
            : "保密"
      }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="mobilePhone" label="手机" />
          <el-table-column label="所在组织" align="center">
            <template slot-scope="scope" sortable>
              <span>{{ scope.row.organizeNames && scope.row.organizeNames.toString() }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="department" label="部门" />
          <el-table-column prop="creatorTime" label="创建时间" :formatter="jadp.tableDateFormat" width="120" />
          <!-- <el-table-column
              prop="sortCode"
              label="排序"
              width="70"
              align="center"
            /> -->
          <!-- <el-table-column label="状态" width="70" align="center">
              <template slot-scope="scope">
                <el-switch
                  v-model="scope.row.enabledMark"
                  :active-value="1"
                  :inactive-value="0"
                  @click.native="handleUpdateState(scope.row)"
                  disabled
                  class="table-switch"
                />
              </template>
            </el-table-column> -->
          <el-table-column label="操作" width="120">
            <template slot-scope="scope" v-if="!scope.row.isAdministrator">
              <el-link @click="handleApprove(scope.row)" :underline="false" type="primary">
                {{ $t("user.approved") }}
              </el-link>
              <el-link @click="handleRefused(scope.row)" :underline="false" type="danger" style="margin-left: 10px;">
                {{ $t("user.refused") }}
              </el-link>
            </template>
          </el-table-column>
        </JADP-table>
        <pagination :total="total" :page.sync="params.currentPage" :limit.sync="params.pageSize"
          @pagination="initData" />
      </div>
    </div>
  </div>
</template>
<script>
// import { getDepartmentSelector } from "@/api/permission/department";
import {
  newGetUserList,
  updateUserState,
  delUser,
  examine,
  refuse
} from "@/api/permission/user";

export default {
  name: "permission-user",

  data() {
    return {
      keyword: "",
      treeData: [],
      tableData: [],
      treeLoading: false,
      listLoading: true,
      params: {
        // 不是admin用户，只能查看自己组织架构
        organizeId: this.$store.getters.organizeId,
        keyword: "",
        currentPage: 1,
        pageSize: 20,
        userStatus: 0,
      },
      defaultProps: {
        children: "children",
        label: "fullName",
      },
      total: 0,
      type: "",
      formVisible: false,
      diagramVisible: false,
      resetFormVisible: false,
      authorizeFormVisible: false,
    };
  },
  created() {
    this.initData();

    // this.getOrganizeList();
  },
  methods: {
    showDiagram() {
      this.diagramVisible = true;
      this.$nextTick(() => {
        this.$refs.Diagram.init();
      });
    },
    search() {
      this.params.currentPage = 1;
      this.params.pageSize = 20;
      this.params.sort = "desc";
      this.initData();
    },
    reset() {
      this.params.keyword = "";
      this.search();
    },
    handleApprove(row) {
      examine({ id: row.id, organizeId: this.params.organizeId }).then((res) => {
        this.$message({
          type: res.data ? "success" : "error",
          message: res.msg
        })
        if (res.data) {
          this.search()
        }
      })
    },
    handleRefused(row) {
      refuse({ id: row.id, organizeId: this.params.organizeId }).then((res) => {
        this.$message({
          type: res.data ? "success" : "error",
          message: res.msg
        })
        if (res.data) {
          this.search()
        }
      })
    },

    initData() {
      this.listLoading = true;
      newGetUserList(this.params)
        .then((res) => {
          this.tableData = res.data.list;
          this.total = res.data.pagination.total;
          this.listLoading = false;
        })
        .catch(() => {
          this.listLoading = false;
        });
    },
    handleNodeClick(data) {
      if (this.params.organizeId === data.id) return;
      this.params.organizeId = data.id;
      this.type = data.type;
      this.reset();
    },
    handleAddEdit(id) {
      this.formVisible = true;
      this.$nextTick(() => {
        this.$refs.Form.init(
          id,
          this.type === "department" ? this.params.organizeId : ""
        );
      });
    },
    removeForm(isRefresh) {
      this.formVisible = false;
      if (isRefresh) {
        this.keyword = "";
        this.initData();
      }
    },
    removeAuthorizeForm(isRefresh) {
      this.authorizeFormVisible = false;
      if (isRefresh) {
        this.keyword = "";
        this.initData();
      }
    },
    handleUpdateState(row) {
      if (!this.jadp.hasBtnP("btn_edit"))
        return this.$message.warning(this.$t("common.noPerTip"));
      const txt = row.enabledMark ? "禁用" : "开启";
      this.$confirm(`您确定要${txt}当前用户吗, 是否继续?`, "提示", {
        type: "warning",
      })
        .then(() => {
          updateUserState(row.id).then((res) => {
            this.$message({
              type: "success",
              message: res.msg,
              duration: 1000,
              onClose: () => {
                row.enabledMark = row.enabledMark ? 0 : 1;
              },
            });
          });
        })
        .catch(() => { });
    },
    handleDel(id) {
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        type: "warning",
      })
        .then(() => {
          delUser(id, this.$store.getters.organizeId).then((res) => {
            this.$message({
              type: "success",
              message: res.msg,
              duration: 1500,
              onClose: () => {
                this.initData();
              },
            });
          });
        })
        .catch(() => { });
    },
    handleResetPwd(id, account) {
      this.resetFormVisible = true;
      this.$nextTick(() => {
        this.$refs.ResetPwdForm.init(id, account);
      });
    },
  },
};
</script>